#include <stdio.h>
#include <stdbool.h>
// all function should be used after init_log
// log_enable is deafultly set true, write the log file is enabled
extern bool log_enable;
extern FILE *log_fp;


#define ANSI_FG_BLACK   "\33[1;30m"
#define ANSI_FG_RED     "\33[1;31m"
#define ANSI_FG_GREEN   "\33[1;32m"
#define ANSI_FG_YELLOW  "\33[1;33m"
#define ANSI_FG_BLUE    "\33[1;34m"
#define ANSI_FG_MAGENTA "\33[1;35m"
#define ANSI_FG_CYAN    "\33[1;36m"
#define ANSI_FG_WHITE   "\33[1;37m"
#define ANSI_BG_BLACK   "\33[1;40m"
#define ANSI_BG_RED     "\33[1;41m"
#define ANSI_BG_GREEN   "\33[1;42m"
#define ANSI_BG_YELLOW  "\33[1;43m"
#define ANSI_BG_BLUE    "\33[1;44m"
#define ANSI_BG_MAGENTA "\33[1;35m"
#define ANSI_BG_CYAN    "\33[1;46m"
#define ANSI_BG_WHITE   "\33[1;47m"
#define ANSI_NONE       "\33[0m"

#define ANSI_FMT(str, fmt) fmt str ANSI_NONE

#define log_write(...) \
  do { \
    extern FILE* log_fp; \
    extern bool log_enable; \
    if (log_enable) { \
       fprintf(log_fp, __VA_ARGS__); \
       fflush(log_fp); \
    } \
	}while(0) \

#define _Log(...) \
		do { \
	    printf(__VA_ARGS__); \
	    log_write(__VA_ARGS__); \
	  } while (0)

#define Log(format, ...) \
	  _Log(ANSI_FMT("[%s:%d %s] " format, ANSI_FG_BLUE) "\n", \
       __FILE__, __LINE__, __func__, ## __VA_ARGS__)

void init_log(const char*log_file);

